package net.jumpengine;

import net.jumpengine.inter.ILog;
import net.jumpengine.util.UComm;

/**
 * 日志接口的简单实现,level 5-1,error&gt;warn&gt;debug&gt;info&gt;log,level越大日志越少,默认为1
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020-02-20 14:58<br />
 * @date
 */
public class LogImpl implements ILog {
	private int level=1;
	
	@Override
	public void log(CharSequence sequence, Throwable throwable) {
		if(level<=1)error(sequence, throwable);
	}
	@Override
	public void log(Class<?> c, Object... objs) {
		if(level<=1)error(c, objs);
	}
	@Override
	public void log(Object... objs) {
		if(level<=1)error(objs);
	}
	
	@Override
	public void info(CharSequence sequence, Throwable throwable) {
		if(level<=2)error(sequence, throwable);
	}
	@Override
	public void info(Class<?> c, Object... objs) {
		if(level<=2)error(c, objs);
	}
	@Override
	public void info( Object... objs) {
		if(level<=2)error(objs);
	}

	@Override
	public void debug(CharSequence sequence, Throwable throwable) {
		if(level<=3)error(sequence, throwable);
	}
	@Override
	public void debug(Class<?> c, Object... objs) {
		if(level<=3)error(c, objs);
	}
	@Override
	public void debug(Object... objs) {
		if(level<=3)error(objs);
	}

	@Override
	public void warn(CharSequence sequence, Throwable throwable) {
		if(level<=4)error(sequence, throwable);
	}
	@Override
	public void warn(Class<?> c, Object... objs) {
		if(level<=4)error(c, objs);
	}
	@Override
	public void warn(Object... objs) {
		if(level<=4)error(objs);
	}
	
	@Override
	public void error(CharSequence sequence, Throwable throwable) {
		System.out.print(UComm.nowString());
		System.out.print(' ');
		System.out.println(sequence);
		throwable.printStackTrace(System.out);
	}
	@Override
	public void error(Class<?> c, Object... objs) {
		System.out.print(UComm.nowString());
		System.out.print(' ');
		System.out.print('{'+c.getName()+'}');
		System.out.println(UComm.arrToStr(objs));
	}
	@Override
	public void error(Object... objs) {
		System.out.print(UComm.nowString());
		System.out.print(" ");
		System.out.println(UComm.arrToStr(objs));
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		if(level<1 || level>5) {
			warn(this.getClass().getName(),"[setLevel(object)]参数:",level,"小于1或大于5,设置失败!");
			return;
		}
		this.level = level;
	}
	
	public void setLevel(Object level) {
		if(level!=null && level instanceof Integer) {
			Integer val=(Integer)level;
			if(val<1 || val>5) {
				warn(this.getClass().getName(),"[setLevel(object)]参数:",level,"小于1或大于5,设置失败!");
				return;
			}
			this.level = (Integer)level;
		}else {
			warn(this.getClass().getName(),"[setLevel(object)]参数:",level,"为空或不为Integer,设置失败!");
		}
	}
}
